package com.example.volleysample;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

/**
 * Volley框架及封装及图片请求方式
 */
public class MainActivity extends Activity implements View.OnClickListener, IResponseListener {
    private static final String TAG = "MainActivity";
    private RequestQueue mQueue;
    private HttpUtil mHttpUtil;
    private ImageView mImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_getrequest).setOnClickListener(this);
        findViewById(R.id.btn_postrequest).setOnClickListener(this);
        findViewById(R.id.btn_imagerequest).setOnClickListener(this);
        findViewById(R.id.btn_imageloader).setOnClickListener(this);
        mImage = (ImageView) findViewById(R.id.iv_image);
        mQueue = Volley.newRequestQueue(this);
        mHttpUtil = new HttpUtil();
        mHttpUtil.setListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_getrequest:
                getRequest();
                break;
            case R.id.btn_postrequest:
                postRequest();
                break;
            case R.id.btn_imagerequest:
                imageRequest();
                break;
            case R.id.btn_imageloader:
                imageLoader();
                break;
        }
    }

    private void imageLoader() {
        RequestQueue queue = Volley.newRequestQueue(this);
        String urlPath = "http://img2.everychina.com/img/d1/d7/144a8de7c562219d32d640d3f1ea-600x400c1-c847/teddy_bear_toy_doll.jpg";
        ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }
        });
        ImageLoader.ImageListener imageListener = imageLoader.getImageListener(mImage, R.mipmap.ic_launcher, R.mipmap.ic_launcher_round);
        imageLoader.get(urlPath, imageListener);
    }

    private void imageRequest() {
        RequestQueue queue = Volley.newRequestQueue(this);
        String urlPath = "http://img2.everychina.com/img/d1/d7/144a8de7c562219d32d640d3f1ea-600x400c1-c847/teddy_bear_toy_doll.jpg";
        ImageRequest request = new ImageRequest(urlPath,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {
                        mImage.setImageBitmap(response);
                    }
                }, 0, 0, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_4444,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i(TAG, "onErrorResponse: " + error.getLocalizedMessage());
                    }
                });
        queue.add(request);
    }

    private void postRequest() {

//        StringRequest request = new StringRequest(Request.Method.POST, "http://mall.520it.com/login", new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                Log.i(TAG, "POST onResponse: " + response);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        }) {
//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                HashMap<String, String> paramsMap = new HashMap<>();
//                paramsMap.put("username", "2");
//                paramsMap.put("pwd", "123456");
//                return paramsMap;
//            }
//        };
//        mQueue.add(request);
        String urlPath = "http://mall.520it.com/login";
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("username", "2");
        paramsMap.put("pwd", "123456");
        mHttpUtil.doPost(IdiyMessage.JDLODING_ACTION, urlPath, paramsMap);
    }

    private void getRequest() {
//        //传递一个URL对象----》String类型的JSON语句
//        StringRequest request = new StringRequest("https://www.baidu.com/", new Response.Listener<String>() {
//            //该方法是在主线程中执行的
//            @Override
//            public void onResponse(String response) {
//                Log.i(TAG, "GET onResponse: " + response);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        //一般 一个应用多个请求---》请求队列里面去
//        mQueue.add(request);

        mHttpUtil.doGet(IdiyMessage.BAIDU_ACTION, "https://www.baidu.com/");
    }

    @Override
    public void onNetworkResponsed(int action, String jsonStr) {
        switch (action) {
            case IdiyMessage.BAIDU_ACTION:
                Log.i(TAG, "baidu " + jsonStr);
                break;
            case IdiyMessage.JDLODING_ACTION:
                Log.i(TAG, "JD登录: " + jsonStr);
                break;
        }
    }
}
